const createError = require('http-errors');
const express = require('express');

const path = require('path');
const cookieParser = require('cookie-parser');
// 日志
const logger = require('morgan');
// 跨域
const cors = require('cors');
// 解析参数
const bodyParser = require('body-parser');
// server
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))

// 封装res.send 中间件
app.use((req, res, next) => {
  // 默认 err 为 0, success 为 1
  res.cc = function (err, state = 0) {
    res.send({
      state,
      msg: err instanceof Error ? err.message : err
    })
  }
  next();
})

const intercep = require('./utils/interceptor');
// 拦截器
// app.use(intercep.interceptor);

// 路由
const userRouter = require('./routes/users');
const adminRouter = require('./routes/admin');
const stuRouter = require('./routes/student');
const teacherRouter = require('./routes/teacher');

app.use('/users', userRouter);
app.use('/admin', adminRouter);
app.use('/student', stuRouter);
app.use('/teacher', teacherRouter);

module.exports = app;